<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

/**
 * App\OrderPay
 *
 * @property int $order_pay_id
 * @property int $user_id
 * @property string $pay_sn 支付单号，(可作为总订单号)
 * @property string $order_ids 支付订单合集
 * @property string $create_time 创建时间啊
 * @property int $pay_status 是否已经支付，1：已支付，0未支付
 * @property float $pay_price 支付金额
 * @property string $subject 支付标题
 * @property string $desc 支付描述
 * @property string|null $log 支付平台回调历史记录
 * @property string|null $pay_type 支付类型
 * @property string|null $pay_num 支付单流水号
 * @property string|null $pay_time 支付时间
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay whereCreateTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay whereDesc($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay whereLog($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay whereOrderIds($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay whereOrderPayId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay wherePayNum($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay wherePayPrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay wherePaySn($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay wherePayStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay wherePayTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay wherePayType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay whereSubject($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay whereUserId($value)
 * @mixin \Eloquent
 * @property string|null $remark
 * @property string|null $paid_at 支付时间
 * @property string|null $payment_method 支付方式
 * @property string|null $payment_no 支付平台订单号
 * @property string|null $refund_no
 * @property int|null $closed 订单是否已关闭
 * @property string|null $ship_status 物流状态
 * @property string|null $ship_data 物流数据
 * @property string|null $extra 其他额外数据
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay whereClosed($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay whereExtra($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay wherePaidAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay wherePaymentMethod($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay wherePaymentNo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay whereRefundNo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay whereRemark($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay whereShipData($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay whereShipStatus($value)
 * @property int|null $address_id 地址id
 * @property-read \App\OrderAddress|null $address
 * @property-read \App\OrderCoupon $coupons
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\OrderGoods[] $goods
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay whereAddressId($value)
 * @property float|null $total_money 总计
 * @property int|null $total_num 商品数量
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay whereTotalMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay whereTotalNum($value)
 * @property string|null $refund_status 退款状态
 * @property string|null $express_company 物流公司
 * @property string|null $express_no 物流单号
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay whereExpressCompany($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay whereExpressNo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay whereRefundStatus($value)
 * @property int|null $is_recharge 是否收购
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay whereIsRecharge($value)
 * @property string|null $mail_no
 * @property string|null $pdf_info
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay whereMailNo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay wherePdfInfo($value)
 * @property float|null $pay_balance 零钱支付的钱
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay wherePayBalance($value)
 * @property string|null $refund_time 退款时间
 * @property string|null $leader_time 绑定时间
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay whereLeaderTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderPay whereRefundTime($value)
 */
class OrderPay extends Model
{
    protected $table='order_pay';
    public $timestamps=false;
    protected $primaryKey='order_pay_id';

    const REFUND_STATUS_PENDING = 'pending';
    const REFUND_STATUS_APPLIED = 'applied';

    //支付方式
    const  PAY_BALANCE='balance';
    const  PAY_ALI='alipay';
    const  PAY_WECHAT='wechat';
    const TRANSIT='transit';//运输中
    const SIGNFAIL='signfail';//签收异常
    const SIGNED='signed';//已签收
    const SHIP_STATUS_SURE_ORDER='sure';
    const SHIP_STATUS_PENDING = 'pending';
    const SHIP_STATUS_DELIVERED = 'delivered';
    const SHIP_STATUS_RECEIVED = 'received';
    const SHIP_STATUS_CLOSED='closed';
    public static $expressStaTusMAP=[
        self::TRANSIT=>'运输中',
        self::SIGNFAIL=>'签收异常',
        self::SIGNED=>'已签收',
    ];
    public static $refundStatusMap = [
        self::REFUND_STATUS_PENDING    => '未退款',
        self::REFUND_STATUS_APPLIED    => '退款成功',
    ];
    public static  $payTypeMap=[
        self::PAY_BALANCE=>'余额支付',
        self::PAY_ALI=>'支付宝支付',
        self::PAY_WECHAT=>'微信支付',

    ];

    public static $shipStatusMap = [
        self::SHIP_STATUS_SURE_ORDER=>'待接单',
        self::SHIP_STATUS_PENDING   => '待发货',
        self::SHIP_STATUS_DELIVERED => '已发货',
        self::SHIP_STATUS_RECEIVED  => '已收货',
        self::SHIP_STATUS_CLOSED=>'订单已关闭'
    ];
    //关联地址
    public function address(){

        return $this->belongsTo('App\OrderAddress','address_id');
    }
    //关联商品
    public function goods(){
        return $this->hasMany('App\OrderGoods','order_pay_id');
    }
    //关联优惠券
    public function coupons(){
        return $this->hasOne('App\OrderCoupon','order_pay_id');
    }
    public function setExtraAttribute($value)
    {
        $this->attributes['extra']= json_encode($value);
    }

    public function setPdfInfoAttribute($value)
    {
        $this->attributes['pdf_info']= json_encode($value);
    }
    public function setShipDataAttribute($value)
    {
        $this->attributes['ship_data']=json_encode($value);
    }
    public function getShipDataAttribute($value){
        if($value){
            return json_decode($value,true);
        }
        return $value;
    }
    public function getPdfInfoAttribute($value){
        if($value){

            return json_decode($value,true);
        }else{
            return $value;
        }
    }
}
